#include "I18nStrings.h"
#include "xml_util.h"
#include <iostream>
#include <sstream>
#include <stack>
#include <string.h>

namespace phnq
{
	namespace phnqlets
	{
		I18nStrings::I18nStrings(string stringsFilePath)
		{
			phnq::kit::xml::SAXHelper helper(this);
			helper.parse(stringsFilePath);
		}

		I18nStrings::~I18nStrings()
		{
			// a little destructor action
		}

		void I18nStrings::startTag(string tagName, string path, map<string,string> attrs)
		{
			if(path == "/strings/string")
			{
				string k = attrs["key"];
				string v = attrs["value"];
				this->strings[k] = new string(v);
			}
		}
		
		void I18nStrings::charData(string data, string path, map<string,string> attrs)
		{
			if(path == "/strings/string")
			{
				string k = attrs["key"];
				this->strings[k] = new string(data);
			}
		}

		set<string> I18nStrings::getAllKeys()
		{
			set<string> allKeys;
			for(map<string,string*>::iterator it=this->strings.begin(); it!=this->strings.end(); it++)
			{
				allKeys.insert(it->first);
			}
			return allKeys;
		}
		
		bool I18nStrings::containsKey(string key)
		{
			return this->strings.find(key) != this->strings.end();
		}
		
		string* I18nStrings::get(string key)
		{
			map<string,string*>::iterator it = this->strings.find(key);
			if(it == this->strings.end())
				return NULL;
			else
				return it->second;
		}
	}
}
